﻿using StartEAS_API.Utils;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace StartsEAS_API.DAL
{
    public class LoginDAL
    {
        private readonly StarsEASContext _context;
        public LoginDAL(StarsEASContext context) {
            _context = context;
        }
        public ResultUtils GetStudent(string username,string pwd) {
            StarsEASContext _context = new StarsEASContext();
            Guid token = Guid.NewGuid();
            User u = _context.Users.FirstOrDefault(u => u.Username == username && u.Password == Utils.GenerateMD5(pwd));
            if (u == null || u.PositionId != 1)
            {
                AppLog.WriteError($"登录异常---账户{username}尝试使用{pwd}登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "您的账户或密码错误" };
            }
            var stu = (from user in _context.Users
                       join student in _context.Students on user.Username equals student.StuId
                       join classes in _context.Classes on student.ClassId equals classes.ClassId
                       join college in _context.Colleges on classes.CollegeId equals college.CollegeId
                       join professional in _context.Professionals on classes.ProfessionalId equals professional.ProfessionalId
                       join worker in _context.Workers on classes.WorkerId equals worker.WorkerId
                       where user.Username == username &&
                       user.Password == Utils.GenerateMD5(pwd)
                       select new
                       {
                           StuName = student.StuName,
                           StuAge = student.StuAge,
                           StubackTime = student.StubackTime.ToString("yyyy-MM-dd"),
                           StuCarid = student.StuCarid,
                           StuEmail = student.StuEmail,
                           StuPhone = student.StuPhone,
                           StuSex = student.StuSex,
                           StuId = student.StuId,
                           StuClassId = student.ClassId,
                           Grade = classes.Grade,
                           TheCounselorId = classes.WorkerId,
                           TheCounselorName = worker.WorkerName,
                           TheCounselorPhone = worker.WorkerPhone,
                           PoliticalLandscape = student.PoliticalLandscape,
                           NativePlace = student.NativePlace,
                           National = student.National,
                           ProfessionalName = professional.ProfessionalName,
                           CollegeName = college.CollegeName,
                           StateOfTheSchool = student.StateOfTheSchool,
                           Token = token
                       }).FirstOrDefault();
            if (stu != null) {
               var use =  _context.Users.Where<User>(u=>u.Username == stu.StuId).FirstOrDefault();
                use.Token = token.ToString();
                _context.SaveChanges();
                AppLog.WriteInfo($"账号：{username},姓名：{stu.StuName}尝试以学生身份登录成功,token：{token}", true);
                return new ResultUtils() { code = 200, message = stu };
            }
            AppLog.WriteError($"账号：{username},姓名：{stu.StuName}尝试以学生身份登录失败", true);
            return new ResultUtils() { code=500,message= "您的账号或密码错误" };
        }
        public ResultUtils GetTeacher(string username, string pwd)
        {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.FirstOrDefault(u => u.Username == username && u.Password == Utils.GenerateMD5(pwd));
            if (user == null || user.PositionId == 1)
            {
                AppLog.WriteError($"登录异常---账户{username}尝试使用{pwd}登录但登录失败", true);
                return new ResultUtils() { code = 500, message = "您的账户或密码错误" };
            }
            Guid token = Guid.NewGuid();
            var worker = (from wor in _context.Workers
                        join use in _context.Users on wor.WorkerId equals use.Username
                        where use.Username == username && use.Password == Utils.GenerateMD5(pwd)
                        select new
                        {
                            WorkerId = wor.WorkerId,
                            WorkerName = wor.WorkerName,
                            WorkerAge = wor.WorkerAge,
                            WorkerbackTime = wor.WorkerbackTime,
                            WorkerCarid = wor.WorkerCarid,
                            WorkerEmail = wor.WorkerEmail,
                            WorkerLocal = wor.WorkerLocal,
                            WorkerPhone = wor.WorkerPhone,
                            WorkerSex = wor.WorkerSex,
                            PositionId = wor.PositionId,
                            Token = token
                        }).FirstOrDefault();
            Model.Models.Class classInfo = _context.Classes.FirstOrDefault(c=>c.WorkerId==worker.WorkerId);
            if (user.PositionId == 6 || classInfo==null)
            {
                if (worker != null)
                {
                    var use = _context.Users.Where<User>(u => u.Username == worker.WorkerId).FirstOrDefault();
                    use.Token = token.ToString();
                    _context.SaveChanges();
                    AppLog.WriteInfo($"账号：{username}，姓名：{worker.WorkerName}尝试以教职工身份登录成功,该老师职位为{user.PositionId},token：{token}", true);
                    return new ResultUtils() { code = 200, message = worker };
                }
                AppLog.WriteError($"账号：{username}，姓名：{worker.WorkerName}尝试以教职工身份登录失败,该老师职位为{user.PositionId}", true);
                return new ResultUtils() { code = 500, message = "您的账号或密码错误" };
            }
            else
            {
                var work = (from wor in _context.Workers
                            join use in _context.Users on wor.WorkerId equals use.Username
                            join classes in _context.Classes on use.Username equals classes.WorkerId
                            join college in _context.Colleges on classes.CollegeId equals college.CollegeId
                            join professional in _context.Professionals on classes.ProfessionalId equals professional.ProfessionalId
                            where use.Username == username && use.Password == Utils.GenerateMD5(pwd)
                            select new
                            {
                                WorkerId = wor.WorkerId,
                                WorkerName = wor.WorkerName,
                                WorkerAge = wor.WorkerAge,
                                WorkerbackTime = wor.WorkerbackTime,
                                WorkerCarid = wor.WorkerCarid,
                                WorkerEmail = wor.WorkerEmail,
                                WorkerLocal = wor.WorkerLocal,
                                WorkerPhone = wor.WorkerPhone,
                                WorkerSex = wor.WorkerSex,
                                PositionId = wor.PositionId,
                                ClassId = classes.ClassId,
                                Grade = classes.Grade,
                                CollegeName = college.CollegeName,
                                ProfessionalName = professional.ProfessionalName,
                                Token = token
                            }).FirstOrDefault();
                if (work != null)
                {
                    var use = _context.Users.Where<User>(u => u.Username == work.WorkerId).FirstOrDefault();
                    use.Token = token.ToString();
                    _context.SaveChanges();
                    AppLog.WriteInfo($"账号：{username}，姓名：{worker.WorkerName}尝试以教职工身份登录成功,token：{token}", true);
                    return new ResultUtils() { code = 200, message = work };
                }
                AppLog.WriteError($"账号：{username}，姓名：{worker.WorkerName}尝试以教职工身份登录失败", true);
                return new ResultUtils() { code = 500, message = "您的账号或密码错误" };
            }
        }
    }
}
